﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading;

namespace ID3
{
    /// <summary>
    /// Classe que exemplifica a utilização do ID3
    /// </summary>
    class ID3Sample
    {

        public static void printNode(TreeNode root, string tabs)
        {
            Console.WriteLine(tabs + '|' + root.attribute + '|');

            if (root.attribute.values != null)
            {
                for (int i = 0; i < root.attribute.values.Length; i++)
                {
                    Console.WriteLine(tabs + "\t" + "<" + root.attribute.values[i] + ">");
                    TreeNode childNode = root.getChildByBranchName(root.attribute.values[i]);
                    printNode(childNode, "\t" + tabs);
                }
            }
            //Thread.Sleep(10000);
        }


        static DataTable getDataTable()
        {
            DataTable result = new DataTable("samples");
            DataColumn column = result.Columns.Add("I1");
            column.DataType = typeof(string);

            column = result.Columns.Add("I2");
            column.DataType = typeof(string);

            column = result.Columns.Add("I3");
            column.DataType = typeof(string);

            column = result.Columns.Add("I4");
            column.DataType = typeof(string);

            column = result.Columns.Add("I5");
            column.DataType = typeof(string);

            column = result.Columns.Add("I6");
            column.DataType = typeof(string);

            column = result.Columns.Add("result");
            column.DataType = typeof(bool);

            result.Rows.Add(new object[] { "0", "1", "1", "1", "0", "0", false }); //I1, I2, I3, I4, I5, I6, T
            result.Rows.Add(new object[] { "0", "1", "1", "0", "1", "1", false }); //I1, I2, I3, I4, I5, I6, T
            result.Rows.Add(new object[] { "0", "0", "0", "1", "0", "1", false }); //I1, I2, I3, I4, I5, I6, T
            result.Rows.Add(new object[] { "0", "1", "0", "1", "0", "1", false }); //I1, I2, I3, I4, I5, I6, T
            result.Rows.Add(new object[] { "0", "0", "0", "1", "1", "0", false }); //I1, I2, I3, I4, I5, I6, T
            result.Rows.Add(new object[] { "0", "1", "1", "0", "1", "1", false }); //I1, I2, I3, I4, I5, I6, T
            result.Rows.Add(new object[] { "1", "1", "1", "1", "0", "1", true }); //I1, I2, I3, I4, I5, I6, T
            result.Rows.Add(new object[] { "0", "0", "0", "0", "1", "0", false }); //I1, I2, I3, I4, I5, I6, T
            result.Rows.Add(new object[] { "1", "0", "0", "0", "1", "1", true }); //I1, I2, I3, I4, I5, I6, T
            result.Rows.Add(new object[] { "1", "1", "1", "1", "0", "0", true }); //I1, I2, I3, I4, I5, I6, T
            result.Rows.Add(new object[] { "1", "1", "0", "0", "1", "1", true }); //I1, I2, I3, I4, I5, I6, T
            result.Rows.Add(new object[] { "1", "0", "1", "0", "1", "0", true }); //I1, I2, I3, I4, I5, I6, T
            result.Rows.Add(new object[] { "1", "0", "1", "1", "0", "0", true }); //I1, I2, I3, I4, I5, I6, T
            result.Rows.Add(new object[] { "1", "0", "1", "0", "0", "0", true }); //I1, I2, I3, I4, I5, I6, T

            return result;

        }

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        /// 
        [STAThread]
        static void Main(string[] args)
        {
            Console.WriteLine("Executando algoritmo ID3...");
            Attribute I1 = new Attribute("I1", new string[] { "0", "1" });
            Attribute I2 = new Attribute("I2", new string[] { "0", "1" });
            Attribute I3 = new Attribute("I3", new string[] { "0", "1" });
            Attribute I4 = new Attribute("I4", new string[] { "0", "1" });
            Attribute I5 = new Attribute("I5", new string[] { "0", "1" });
            Attribute I6 = new Attribute("I6", new string[] { "0", "1" });

            Attribute[] attributes = new Attribute[] { I1, I2, I3, I4, I5, I6 };

            DataTable samples = getDataTable();

            DecisionTreeID3 id3 = new DecisionTreeID3();
            TreeNode root = id3.mountTree(samples, "result", attributes);

            printNode(root, "");

            Console.ReadKey();
        }
    }
}
